package com.njwb.Swing.Bill;

import com.njwb.Util.UtilService;
import com.njwb.pojo.Bill;
import com.njwb.pojo.Supplier;

import javax.swing.*;
import java.awt.*;
import java.sql.Date;
import java.util.ArrayList;

public class EditBillDialog extends JDialog {
    private JTextField productField;
    private JTextField quantityField;
    private JTextField unitField;
    private JTextField amountField;
    private JComboBox<String> isPaidBox;
    private JComboBox<String> supplierBox;
    private JTextField descriptionField;
    private JTextField dateField;
    private JButton confirmButton;
    private JButton cancelButton;

    private UtilService utilService;
    private Runnable refreshCallback;
    private Bill bill; // 要修改的账单

    public EditBillDialog(Frame owner, UtilService utilService, Bill bill, Runnable refreshCallback) {
        super(owner, "修改账单", true);
        this.utilService = utilService;
        this.bill = bill;
        this.refreshCallback = refreshCallback;

        setSize(400, 450);
        setLocationRelativeTo(owner);
        setLayout(new GridLayout(9, 2, 5, 5));

        add(new JLabel("商品名称:"));
        productField = new JTextField(bill.getProduct_name());
        add(productField);

        add(new JLabel("交易数量:"));
        quantityField = new JTextField(String.valueOf(bill.getQuantity()));
        add(quantityField);

        add(new JLabel("交易单位:"));
        unitField = new JTextField(bill.getUnit());
        add(unitField);

        add(new JLabel("交易金额:"));
        amountField = new JTextField(String.valueOf(bill.getAmount()));
        add(amountField);

        add(new JLabel("是否付款:"));
        isPaidBox = new JComboBox<>(new String[]{"未付款", "已付款"});
        isPaidBox.setSelectedIndex(bill.getIs_paid() ? 1 : 0);
        add(isPaidBox);

        add(new JLabel("所属供应商:"));
        supplierBox = new JComboBox<>();
        ArrayList<Supplier> suppliers = utilService.getAllSupplier();
        for (Supplier s : suppliers) {
            supplierBox.addItem(s.getName());
        }
        supplierBox.setSelectedItem(bill.getSupplier_name());
        add(supplierBox);

        add(new JLabel("商品描述:"));
        descriptionField = new JTextField(bill.getProduct_description());
        add(descriptionField);

        add(new JLabel("交易时间 (yyyy-mm-dd):"));
        dateField = new JTextField(bill.getBill_time().toString());
        add(dateField);

        confirmButton = new JButton("确认修改");
        cancelButton = new JButton("取消");
        add(confirmButton);
        add(cancelButton);

        // 确认按钮事件
        confirmButton.addActionListener(e -> {
            try {
                String product_name = productField.getText().trim();
                int quantity = Integer.parseInt(quantityField.getText().trim());
                String unit = unitField.getText().trim();
                Double amount = Double.parseDouble(amountField.getText().trim());
                Boolean is_paid = isPaidBox.getSelectedIndex() == 1;
                String supplier_name = (String) supplierBox.getSelectedItem();
                String product_description = descriptionField.getText().trim();
                Date bill_time = Date.valueOf(dateField.getText().trim());

                boolean success = utilService.modifyBill(
                        bill.getBill_id(), product_name, quantity, unit, amount,
                        is_paid, supplier_name, product_description, bill_time
                );
                if (success) {
                    JOptionPane.showMessageDialog(this, "修改账单成功！");
                    dispose();
                    if (refreshCallback != null) refreshCallback.run();
                } else {
                    JOptionPane.showMessageDialog(this, "修改账单失败！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "输入有误，请检查数据格式！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        cancelButton.addActionListener(e -> dispose());
    }
}
